<template>
  <a-modal
    :title=" model!=null ? '修改招聘' : '新增招聘'"
    :width="1224"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="主键ID">
          <a-input v-decorator="['id', { initialValue: null }]" disabled />
        </a-form-item>
		<a-form-item v-show="false" label="状态">
		  <a-input v-decorator="['status', { initialValue: '0' }]" disabled />
		</a-form-item>
		<a-form-item label="标题">
		  <a-input v-decorator="['title',{rules: [{required: true, message: '请输入标题！'}]}]"  />
		</a-form-item>
		<a-form-item label="招聘岗位">
		  <a-input v-decorator="['job',{rules: [{required: true, max: 50, message: '最长50个字符！'}]}]"  />
		</a-form-item>
		<a-form-item label="招聘人数">
		  <a-input v-decorator="['nums',{rules: [{required: true,  message: '请输入！'}]}]"  type="number"/>
		</a-form-item>
		<a-form-item label="招聘薪资">
		  <a-input v-decorator="['salary',{rules: [{required: true,  message: '请输入！'}]}]"  type="number"/>
		</a-form-item>
		<a-form-item label="招聘截至日期">
			 <a-date-picker v-decorator="['endDate',{rules: [{required: true, message: '请选择!'}]}]"/>
		</a-form-item>
		<a-row :gutter="24">
			<a-col :span="12">
				<a-form-item label="内容">
				  <a-textarea :auto-size="{ minRows: 20, maxRows: 40 }" @change="handleContent()" v-decorator="['content', {rules: [{required: true, message: '请输入内容！'}]}]"/>
				</a-form-item>
			</a-col>
			<a-col :span="12">
				<div v-html="html"></div>
			</a-col>
		</a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import {marked} from 'marked'
import moment from 'moment'
// 表单字段
const fields = ['job', 'id', 'title', 'content', 'status','nums','salary','endDate']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 4 },
        sm: { span: 4 }
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 20 }
      }
    }
    return {
	  html: "",
      form: this.$form.createForm(this)
    }
  },
  created () {
    console.log('custom modal created')
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
	  if(this.model.endDate){
	  	this.model.endDate = moment(new Date(this.model.endDate),'YYYY-MM-DD')
	  }
      this.model && this.form.setFieldsValue(pick(this.model, fields))
	  this.html = ""
	  if(this.model.content){
	  	this.html =  marked.parse(this.model.content)
	  }
    })
  },
  methods: {
	  handleContent(){
		  const v = this.form.getFieldValue('content')
		  if(v){
		  	this.html =  marked.parse(v)
		  }
	  }
  }
}
</script>
